﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class EditorMore : Editor
{
    public const string UnityStr = ".unity";
    [MenuItem("Assets/ZW Export Assets")]
    static void Build()
    {
        if (Selection.objects == null) return;
        List<string> paths = new List<string>();
        foreach (UnityEngine.Object o in Selection.objects)
        {
            paths.Add(AssetDatabase.GetAssetPath(o));
        }

        AssetDatabase.ExportPackage(paths.ToArray(), "Assets/" + Selection.objects[0].name + "andxxx.unitypackage", ExportPackageOptions.IncludeDependencies);
        AssetDatabase.Refresh();
        Debug.Log("Build all Done!");
    }


    public static void CheckSceneSetting(string path)
    {
        List<string> dirs = new List<string>();
        GetDirs(Application.dataPath + "/Scenes/", ref dirs);
        GetDirs(path, ref dirs);
        if (dirs.Count <= 0)
        {
            Debug.LogError("出错了");
        }
        else
        {
            EditorBuildSettingsScene[] newSettings = new EditorBuildSettingsScene[dirs.Count];
            for (int i = 0; i < newSettings.Length; i++)
            {
                newSettings[i] = new EditorBuildSettingsScene(dirs[i], true);
            }
            EditorBuildSettings.scenes = newSettings;
        }
        AssetDatabase.SaveAssets();
        Debug.Log("添加完毕");
    }
    private static void GetDirs(string dirPath, ref List<string> dirs)
    {
        //Debug.Log("GetDirs:dirPath->" + dirPath);
        foreach (string path in Directory.GetFiles(dirPath))
        {
            FileInfo info = new FileInfo(path);

            if (info.FullName.Contains(UnityStr))
            {
                int index = info.FullName.IndexOf("\\Assets");
                if (index > 0 && !info.FullName.EndsWith(".meta"))
                {
                    Debug.Log("找到Scene->" + info.FullName);
                    string sub = info.FullName.Substring(index + 1);
                    dirs.Add(sub);
                }
            }
        }
        if (Directory.GetDirectories(dirPath).Length > 0)
        {
            foreach (string path in Directory.GetDirectories(dirPath))
                GetDirs(@path, ref dirs);
        }
    }
}
//识别场景：[CustomEditor(typeof(UnityEditor.SceneAsset))]

//识别文件夹和一些unity不支持的文件类型：[CustomEditor(typeof(UnityEditor.DefaultAsset))]

//识别图片，txt暂时无方法
[CustomEditor(typeof(DefaultAsset))]
//[CustomEditor(typeof(UnityEditor.SceneAsset))]
public class LuaInspector : Editor
{
    public override void OnInspectorGUI()
    {
        string path = AssetDatabase.GetAssetPath(target);
        GUI.enabled = true;
        if (path.EndsWith(".lua"))
        {
            GUI.enabled = true;
            GUI.backgroundColor = Color.white;
            string luaText = File.ReadAllText(path);
            GUILayout.TextArea(luaText);
        }
        else if (path.EndsWith(".txt"))
        {
            GUI.enabled = true;
            GUI.backgroundColor = Color.white;
            string luaText = File.ReadAllText(path);
            GUILayout.TextArea(luaText);
        }
        else if (path.EndsWith(".unity"))
        {
            GUILayout.Button("该游戏的启动场景");
        }
        else if (!path.Contains("."))//文件夹要放到最后，因为所有的结尾均为空
        {
            if (GUILayout.Button("打开文件夹所在路径"))
            {
                string targetPath = Application.dataPath + "/../" + path;
                Debug.Log(targetPath);

                Application.OpenURL(targetPath);
            }

            // if (GUILayout.Button("SVN更新该文件夹"))
            // {
            //     string targetPath = Application.dataPath + "/../" + path;
            //     Debug.Log(targetPath);
            //
            //     SvnOperation.ExeSvnUpdate(targetPath);
            // }
            // if (GUILayout.Button("SVN显示该文件夹日志"))
            // {
            //     string targetPath = Application.dataPath + "/../" + path;
            //     Debug.Log(targetPath);
            //
            //     SvnOperation.ExeSvnShowLog(targetPath);
            // }
            /// 还原代码还是最好手动操作，一键化容易乱来
            //if (GUILayout.Button("SVN还原该文件夹"))
            //{
            //    string targetPath = Application.dataPath + "/../" + path;
            //    Debug.Log(targetPath);

            //    SvnOperation.ExeSvnRevert(targetPath);
            //}

            // if (GUILayout.Button("SVN提交该文件夹"))
            // {
            //     string targetPath = Application.dataPath + "/../" + path;
            //     Debug.Log(targetPath);
            //
            //     SvnOperation.ExeSvnCommit(targetPath);
            // }
            if (GUILayout.Button("清理该文件夹下的无用的材质球属性"))
            {
                if (EditorUtility.DisplayDialog("清理无用材质球", "你确定这个是你自己可以创建的文件夹吗 ？ \n" + path, "是的", "不是，我选择错了"))
                {
                    FindResWindows.ClearMaterialProperties();
                }
            }
            if (GUILayout.Button("检测目录，长宽不为4的倍数的图片"))
            {
                SpriteAtlasEditor.CheckTextureWidthHeight4();
            }
            if (GUILayout.Button("检测目录，长宽不为2的倍数的图片"))
            {
                SpriteAtlasEditor.CheckTextureWidthHeight2();
            }
            if (GUILayout.Button("检测目录，未被引用的资源有哪些"))
            {
                FindResWindows.FindReferencesInFolder();
            }
            if (GUILayout.Button("添加该目录的场景到设置中"))
            {
                string targetPath = Application.dataPath + "/../" + path;
                EditorMore.CheckSceneSetting(targetPath);
            }
        }
    }
}
